package com.my.lucky.repository;

import com.my.lucky.entity.GaiNianInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Repository
public interface GainianInfoRepository extends JpaRepository<GaiNianInfo, Long> {

    @Query("SELECT g.gainianCode as gainianCode, g.gainianName as gainianName, " +
            "SUM(g.sealOrderAmount) as totalAmount " +
            "FROM GaiNianInfo g " +
            "WHERE g.createTime >= :startTime AND g.createTime <= :endTime " +
            "GROUP BY g.gainianCode, g.gainianName")
    List<Map<String, Object>> sumSealOrderAmountByTimeRange(LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 删除recordTime大于当天0点的涨停股票记录
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM GaiNianInfo s WHERE s.createTime > CURRENT_DATE")
    void deleteByCreateTimeAfterToday();


    @Query("SELECT s FROM GaiNianInfo s WHERE s.createTime>=:startDate")
    List<GaiNianInfo> findByCreateTime(@Param("startDate") LocalDateTime startDate);


    @Query("SELECT s FROM GaiNianInfo s WHERE s.createTime>=:startDate and s.createTime<=:endTime order by s.createTime desc ")
    List<GaiNianInfo> findByCreateTimeBetween(@Param("startDate") LocalDateTime startDate, @Param("endTime") LocalDateTime endTime);


    @Query("SELECT s FROM GaiNianInfo s WHERE s.createTime >= :startDate AND s.createTime <= :endTime AND s.gainianName = :code order by s.createTime desc ")
    List<GaiNianInfo> findByCreateTimeBetweenAndCode(
            @Param("startDate") LocalDateTime startDate,
            @Param("endTime") LocalDateTime endTime,
            @Param("code") String code);
}
